package com.fruits.login.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.fruits.login.model.ReturnMsg;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

/**
 * 自定义参数异常处理
 */
@Component
public class ParameterError {


    public ReturnMsg parameterErrors(BindingResult errors) {
        ReturnMsg msg = new ReturnMsg();
        JSONArray result = new JSONArray();
        msg.setState(3);
        errors.getAllErrors().stream().forEach(error -> {
            result.add(error.getDefaultMessage());
        });
        msg.setResult(result);
        return msg;
    }
}
